'use strict'

const _ = require('lodash')
const moment = require('moment')
const Env = use('Env')
const { AliYunService } = require('../../Services')
const { Address } = require("../../Models");
class AddressController {
  async index({request, response}) {
    const { search } = request.all()
    let coll = Address.query()
    if (search) {
      coll.whereRaw(
        'centerName like ? or province like ? or city like ? or district like ?',
        [`%${search}%`, `%${search}%`, `%${search}%`, `%${search}%`]
      )
    }
    coll = await coll.orderBy('sort', 'asc').fetch()
    response.json(coll)
  }
  async addressCity({request, response}) {
    const { search } = request.all()
    let coll = Address.query()
    if (search) {
      coll.whereRaw(
        'province like ? or city like ?',
        [`%${search}%`, `%${search}%`]
      )
    }
    coll = await coll.fetch()
    response.json(coll)
  }
  async addressListByCityName({request, response}) {
    const { search, type } = request.all()
    let coll = Address.query()
    if (type > 0) {
      coll.where('centerType', type)
    }
    if (search) {
      coll.whereRaw(
        'city like ?',
        [`%${search}%`]
      )
    }
    coll = await coll.fetch()
    response.json(coll)
  }
  async addressByCityName({request, response}) {
    const { search, type } = request.all()
    let coll = Address.query()
    if (type > 0) {
      coll.where('centerType', type)
    }
    if (search) {
      coll.whereRaw(
        'city like ?',
        [`%${search}%`]
      )
    }
    coll = await coll.distinct('city').pluck('city')
    response.json(coll)
  }
}
module.exports = AddressController
